package controller

import (
	"hft/Database/admin"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"

	"hft/Http/Models/request"
	models "hft/Http/Models/request"

	"github.com/gin-gonic/gin"
)

type Role struct{}

func (r *Role) AddRole(c *gin.Context) {

	var role *admin.Role

	c.ShouldBindJSON(&role)

	if err := services.RoleService.AddRole(role); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

func (r *Role) EditRole(c *gin.Context) {
	var role *admin.Role

	c.ShouldBindJSON(&role)

	if err := services.RoleService.EditRole(role); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

func (r *Role) ChangeRoleRoutes(c *gin.Context) {
	var res *models.ResIds
	c.ShouldBindJSON(&res)

	if err := services.RoleService.ChangeRoleRoute(res); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}

}

func (r *Role) ChangeRoleApi(c *gin.Context) {
	var res *models.ResIds
	c.ShouldBindJSON(&res)

	if err := services.RoleService.ChangeRoleApi(res); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}

}

func (r *Role) GetRoleList(c *gin.Context) {

	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	} else {
		if roles, err := services.RoleService.GetRoleList(claims.BaseClaims); err != nil {
			utils.FailWithMessage(err.Error(), c)
			return
		} else {
			utils.OkWithData(roles, c)
			return
		}
	}

}

func (r *Role) ChangePower(c *gin.Context) {
	var power request.Power
	c.ShouldBindJSON(&power)
	if err := services.RoleService.ChangePower(&power); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}

}

var RoleController = new(Role)
